package com.advertise.strutsAction;

import java.util.ArrayList;
import java.util.List;

import com.advertise.model.DHTMLXGridLine;
import com.advertise.model.Role;
import com.advertise.model.User;
import com.advertise.service.RoleService_IFace;
import com.advertise.tool.DhtmlxHelper;
import com.opensymphony.xwork2.ActionSupport;

public class RolePageAction {
	private RoleService_IFace roleService;		//@property 1

	public RoleService_IFace getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService_IFace roleService) {
		this.roleService = roleService;
	}
	
	public String showRolePage(){
		return ActionSupport.SUCCESS;
	}
	
	private Role editRole;

	public Role getEditRole() {
		return editRole;
	}

	public void setEditRole(Role editRole) {
		this.editRole = editRole;
	}
	
	public String createRole(){
		editRole = new Role();
		
		return ActionSupport.SUCCESS;
	}
	
	private int selectedRoleId;
	public int getSelectedRoleId() {
		return selectedRoleId;
	}

	public void setSelectedRoleId(int selectedRoleId) {
		this.selectedRoleId = selectedRoleId;
	}

	public String modifyRole(){
		editRole = roleService.getbyID(selectedRoleId);
		
		return ActionSupport.SUCCESS;
	}
	
	public String saveRole(){
		roleService.save(editRole);
		return ActionSupport.SUCCESS;
	}
	
	private String xmlData;

	public String getXmlData() {
		return xmlData;
	}

	public void setXmlData(String xmlData) {
		this.xmlData = xmlData;
	}
	
	public String getRoleXmldata(){
		
		try{
			List<Role> allRole = roleService.getAll();
			List<DHTMLXGridLine> gridLines = new ArrayList<DHTMLXGridLine>();
			for (Role role : allRole) {
				DHTMLXGridLine line = new DHTMLXGridLine();
				line.addFieldValue("" + role.getRoleId());
	
				line.addFieldValue(role.getRoleName());
	
				line.addFieldValue(role.getRoleDesc());
	
	
				gridLines.add(line);
			}
	
			xmlData = DhtmlxHelper.convertListToGridData(gridLines);
			
			System.out.println(xmlData);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	
			return ActionSupport.SUCCESS;
	}
		
	
	
}
